package www.qiyue.test;


import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import www.qiyue.utils.UUIDGenerator;


@RestController
public class ContractService  {



	@Autowired
	private JdbcTemplate jdbcTemplate;
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	@RequestMapping("time1233")
	public void statusChange() {
		// 定时任务开始执行前更新日志的开始时间
		String starDate = sdf.format(new Date());

		String date = sdf.format(new Date());
		System.out.println("第一步修改");
		// 合同 履行状态 02履行中 未分类 待执行 流程 开始日期 当前日期
		jdbcTemplate
				.update("update ais_contract set status_id='02' where (status_id='00' or status_id='01')  and start_date < '"
						+ date + "'");
		//修改合同下的租赁单元为一出租
		String upUnitSql=" update ais_rent_unit set usage_type_id = '01' where id in (select rent_unit_id from ais_contract where status_id = '02'   union "
				+ "select rent_unit_id from ais_contract_unit where contract_id in (select id from ais_contract where status_id = '02' )) ";
		jdbcTemplate.update(upUnitSql);
		//执行成功后更新日志的结束时间

		changeContRent();
	}

	private void changeContRent() {

//		旅行中合同有租金递增的更新租金递增年租为当前年租金；
		String rentSql = "UPDATE ais_contract a INNER JOIN ( SELECT a.contract_id, a.contract_code, a.annual_rent FROM ais_rentifems a, ais_contract b WHERE a.contract_id = b.id AND a.start_date <= NOW() AND a.end_date >= NOW() AND b.status_id = '02' ) b ON a.id = b.contract_id SET a.annual_rent = b.annual_rent";

		jdbcTemplate.update(rentSql);
		contractType();
	}

	public void contractType() {
		// 定时任务开始执行前更新日志的开始时间
		String starDate = sdf.format(new Date());

		String date = sdf.format(new Date());
		jdbcTemplate.update("update ais_contract set status_id='03' where status_id='02' and case when stop_date is not null then  stop_date < '"+ date + "' else end_date < '"+ date + "' end ");

		//跟新租赁单元
		String upUnitSql=" UPDATE ais_rent_unit SET  usage_type_id = '02',use_people=''  WHERE  id IN (SELECT  rent_unit_id FROM ais_contract WHERE status_id = '03' UNION  SELECT   "+
				" rent_unit_id FROM ais_contract_unit  WHERE  contract_id IN (SELECT  id FROM ais_contract WHERE status_id = '03' ))";
		jdbcTemplate.update(upUnitSql);
		String upUnitsql=" update ais_rent_unit set usage_type_id = '01' where id in (select rent_unit_id from ais_contract where status_id = '02'  union "
				+ "select rent_unit_id from ais_contract_unit where contract_id in (select id from ais_contract where status_id = '02' )) ";
		jdbcTemplate.update(upUnitsql);
		String upUnitsql3=" update ais_rent_unit set use_people='' where usage_type_id = '02' ";
		jdbcTemplate.update(upUnitsql3);
//		更新租赁单元使用人
		String rentUsePeople = "UPDATE ais_rent_unit a INNER JOIN ( SELECT a.rent_unit_id, b.customer_name FROM ais_contract_unit a, ais_contract b WHERE a.contract_id = b.id AND b.status_id = '02' GROUP BY a.rent_unit_id UNION ALL SELECT customer_name, rent_unit_id FROM ais_contract WHERE status_id = '02' GROUP BY rent_unit_id ) t ON a.id = t.rent_unit_id SET a.use_people = t.customer_name WHERE a.usage_type_id = '01'";
		jdbcTemplate.update(rentUsePeople);
		String rentUseProple1 = "update ais_rent_unit a INNER JOIN ais_contract b on a.id = b.rent_unit_id and b.status_id='02' set a.use_people = b.customer_name where a.usage_type_id = '01'";
		jdbcTemplate.update(rentUseProple1);
		System.out.println("更新租赁单元使用人");
		//执行成功后更新日志的结束时间
		//jdbcTemplate.update("update ais_syslog set end_date = '"+sdf.format(new Date())+"' where id = '"+sysId+"' ");
	}

	public void execute(Map map)  {

		Map<String, Object> hashMap = new HashMap<String, Object>(map);

	}


	public void llll(){
		String assign=null;
		String zjl=null;
		String id ="8a8a8a8e6cafdd28016ccd1593460063";
		if (id!=null&&id!="") {
			String sql = "SELECT d.full_name FROM ais_contract a ,ais_rent_unit b ,ais_house c ,ais_company d WHERE a.rent_unit_id=b.id and b.house_id=c.id and c.company = d.id and a.id='"+id+"'";
			String map_cqrs="";

			String rent_id="SELECT a.address FROM ais_house a,ais_rent_unit b " +
					"WHERE a.id=b.house_id and a.id='4028138151aef50c0151aef50c260001' " +
					"and b.id in(select rent_unit_id from ais_contract " +
					"where id='"+id+"' union select rent_unit_id from ais_contract_unit " +
					"where contract_id in (select id from ais_contract where id='"+id+"'))";
			Map<String, Object> map = jdbcTemplate.queryForMap(rent_id);
			Map<String, String> data = new HashMap<String, String>();


		}

	}
	private  Map<String, String> thmap(Map<String, String> data,Boolean b){
		Set<String> keySet = data.keySet();
		Map<String, String> datas = new HashMap<String, String>();
		for (String s : keySet) {
			String values = data.get(s);
			datas.put(s,values);
			datas.put(s+"n",values);
		}
		String ais_contract_mb="";
		if (b){
			ais_contract_mb="SELECT * FROM ais_contract_mb WHERE code='N'";
		}else {
			ais_contract_mb="SELECT * FROM ais_contract_mb WHERE code='Y'";
		}
		List<Map<String, Object>> mapList = jdbcTemplate.queryForList(ais_contract_mb);
		if (mapList!=null&&mapList.size()>0){
			for (Map<String, Object> map : mapList) {
				datas.put(map.get("name").toString(),"//");
			}
		}
		return datas;
	}

	public static void main(String[] args) {
		String[] CN_CHARS = new String[] { "零", "一", "二", "三", "四",
				"五", "六", "七", "八", "九" };
		String s="0,1,2";



		//System.out.println("contentInfo = " + CN_CHARS[]);
	}
	public  String getContentInfo(String content) {
		Pattern regex = Pattern.compile("\\$\\{([^}]*)\\}");
		Matcher matcher = regex.matcher(content);
		StringBuilder sql = new StringBuilder();
		while(matcher.find()) {
			sql.append(matcher.group(1)+",");
		}
		if (sql.length() > 0) {
			sql.deleteCharAt(sql.length() - 1);
		}
		return sql.toString();
	}

}
